<?php

class Guestbook_SetupController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
    
    public function createAction()
    {
        // Initialize the application and bootstrap the database adapter
        defined('APPLICATION_PATH')
            || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
        defined('APPLICATION_ENV')
            || define('APPLICATION_ENV', 'development');
        require_once 'Zend/Application.php';
        $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/modules/guestbook/configs/module.ini'
        );
        $bootstrap = $application->getBootstrap();
        $bootstrap->bootstrap('db');
        $dbAdapter = $bootstrap->getResource('db');
        
        // let the user know whats going on (we are actually creating a
        // database here)
        if ('testing' != APPLICATION_ENV) {
            echo 'Writing Database Guestbook in (control-c to cancel): ' . PHP_EOL;
            for ($x = 5; $x > 0; $x--) {
                echo $x . "\r"; sleep(1);
            }
        }
        
        // Check to see if we have a database file already
        $options = $bootstrap->getOption('resources');
        $dbFile  = $options['db']['params']['dbname'];
        if (file_exists($dbFile)) {
            unlink($dbFile);
        }
        
        // this block executes the actual statements that were loaded from
        // the schema file.
        try {
            $schemaSql = file_get_contents(dirname(__FILE__) . '/../data/db/scripts/schema.sqlite.sql');
            // use the connection directly to load sql in batches
            $dbAdapter->getConnection()->exec($schemaSql);
        
            if ('testing' != APPLICATION_ENV) {
                echo PHP_EOL;
                echo 'Database Created';
                echo PHP_EOL;
            }
            
            if ($withData) {
                $dataSql = file_get_contents(dirname(__FILE__) . '/../data/db/scripts/data.sqlite.sql');
                // use the connection directly to load sql in batches
                $dbAdapter->getConnection()->exec($dataSql);
                if ('testing' != APPLICATION_ENV) {
                    echo 'Data Loaded.';
                    echo PHP_EOL;
                }
            }
            
        } catch (Exception $e) {
            echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
            echo $e->getMessage() . PHP_EOL;
            return false;
        }
    }

}
